import '../../../core/app_export.dart';
import '../../../data/models/auth/user.dart';
import '../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../widgets/app_bar/custom_app_bar.dart';
import '../../../widgets/app_bar/outline_icon_button.dart';
import '../../../widgets/screen_state_widget.dart';
import 'package:flutter/material.dart';

import '../widgets/user_attendance_item_view.dart';
import 'user_attendances_list_cubit.dart';

class UserAttendancesListScreen extends StatelessWidget {
  final User user;
  const UserAttendancesListScreen({
    Key? key, required this.user}):super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;
    var attendancesCubit = UserAttendancesListCubit();
    return Scaffold(
      appBar:_buildAppBar(context:context ,cubit: attendancesCubit),
      body: BlocProvider<UserAttendancesListCubit>(
        create: (context) => attendancesCubit..init( user:user),
        child: BlocConsumer<UserAttendancesListCubit, UserAttendancesListState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = UserAttendancesListCubit.get(context);

            return Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: ScreenStateWidget(
                isDone: state is UserAttendancesListDone,
                isError: false,
                isLoading: state is UserAttendancesListLoading,
                onRefresh: cubit.getAllAttendances,
                onDone: Column(
                  children: [
                    Expanded(
                      child:cubit.attendances.isEmpty?
                      Center(
                       child: Text('لم يتم العثور على تسجيلات حضور',style:theme.textTheme.titleMedium),
                      )
                      :ListView.builder(
                        itemCount:cubit.attendances.length,
                        itemBuilder: (context,index){
                          return UserAttendanceItemView(attendance:cubit.attendances[index],user:user);
                        },
                      ),
                    ),
                  ],
                ),
              ),

            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context, required UserAttendancesListCubit cubit}) {
    return CustomAppBar(
        actions: [

        ],
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "سجل الحضور", margin: EdgeInsets.only(left: 12.h)));
  }

}
